/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package se.ramfelt.psnfriends.util;

import se.ramfelt.psnfriends.R;
import android.content.Context;

/**
 * Source code borrowed from Hudson CI server.
 * @author erik
 *
 */
public class TextUtil {
    private static final long ONE_SECOND_MS = 1000;
    private static final long ONE_MINUTE_MS = 60 * ONE_SECOND_MS;
    private static final long ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
    private static final long ONE_DAY_MS = 24 * ONE_HOUR_MS;
    private static final long ONE_MONTH_MS = 30 * ONE_DAY_MS;
    private static final long ONE_YEAR_MS = 365 * ONE_DAY_MS;

    
    /**
     * Returns a human readable text of the time duration.
     * This version should be used for representing a duration of some activity (like build)
     *
     * @param duration
     *      number of milliseconds.
     * @param context 
     */
    public static String getTimeSpanString(long duration, Context context) {
        // Break the duration up in to units.
        long years = duration / ONE_YEAR_MS;
        duration %= ONE_YEAR_MS;
        long months = duration / ONE_MONTH_MS;
        duration %= ONE_MONTH_MS;
        long days = duration / ONE_DAY_MS;
        duration %= ONE_DAY_MS;
        long hours = duration / ONE_HOUR_MS;
        duration %= ONE_HOUR_MS;
        long minutes = duration / ONE_MINUTE_MS;
        duration %= ONE_MINUTE_MS;
        long seconds = duration / ONE_SECOND_MS;

        if (years > 0)
            return makeTimeSpanString(context, years, R.string.last_seen_year, R.string.last_seen_years);
        else if (months > 0)
            return makeTimeSpanString(context, months, R.string.last_seen_month, R.string.last_seen_months);
        else if (days > 0)
            return makeTimeSpanString(context, days, R.string.last_seen_day, R.string.last_seen_days);
        else if (hours > 0)
            return makeTimeSpanString(context, hours, R.string.last_seen_hour, R.string.last_seen_hours);
        else if (minutes > 0)
            return makeTimeSpanString(context, minutes, R.string.last_seen_minute, R.string.last_seen_minutes);
        else 
            return makeTimeSpanString(context, seconds, R.string.last_seen_second, R.string.last_seen_seconds);
    }

    private static String makeTimeSpanString(Context context,
                                             long number,
                                             int residSingle,
                                             int residSeveral) {
        if (number > 1) {
            return String.format(context.getString(residSeveral), number);
        } else {
            return context.getString(residSingle);
        }
        
            
    }
}
